addpath D:\MRI\spm12\toolbox\CTseg
addpath D:\MRI\spm12
addpath D:\MRIdataset\AFT\_Code


%%
%=======================================================================
% -SPM t-test preprocessing
%=======================================================================
%% copy mwrFDG file to a new folder
probe = 'FDG';
flist = dir('.\sorted\');
for m = 3:length(flist)
    try
        f = flist(m);
        fpath = [f.folder,'\',f.name];
        if isdir(fpath)==1
            raw = [fpath,'\_niigz\',probe, '.nii.gz'];
            if exist(raw)>0
                copyfile(raw, ['.\temp\',probe,'_',f.name,'.nii.gz'])
            end
        end
    catch
        disp('err')
    end
end
%% copy Acetate file to a new folder
probe = 'Acetate';
flist = dir('.\sorted\');
for m = 3:length(flist)
    try
        f = flist(m);
        fpath = [f.folder,'\',f.name];
        if isdir(fpath)==1
            raw = [fpath,'\CTnorm\wr',probe, '.nii'];
            if exist(raw)>0
                copyfile(raw, ['.\temp\PT_wr',probe,'_',f.name,'.nii'])
            end
        end
    catch
        disp('err')
    end
end
%%
spm_check_registration
%% copy mwrTSPO file to a new folder
probe = 'tspo';
flist = dir('.\sorted\');
for m = 3:length(flist)
    f = flist(m);
    fpath = [f.folder,'\',f.name];
    if isdir(fpath)==1
        raw = [fpath,'\normed\wr',probe, '.nii'];
        if exist(raw)>0
            copyfile(raw, ['.\temp\PT_wrtspo_',f.name,'.nii'])
        end
    end
end

%% 
%=======================================================================
% -MASK SMOOTH batch precess
%=======================================================================
%%
mask = 'D:\MRI\spm12\toolbox\mrtool\template\brainmask_mni.nii';

f =  10; % smooth kernal
fwhm = [f f f];
wr_list = spmk_filelist('*.nii');
%%
for i=1:numel(wr_list)
    wr_nii = cell2mat(wr_list(i,1));
    matlabbatch{1}.spm.util.imcalc.input = {
        wr_nii
        mask
        };
    matlabbatch{1}.spm.util.imcalc.output = ['m',wr_nii];
    matlabbatch{1}.spm.util.imcalc.outdir = {''};
    matlabbatch{1}.spm.util.imcalc.expression = 'i1.*(i2>0.0)';
    matlabbatch{1}.spm.util.imcalc.options.dmtx = 0;
    matlabbatch{1}.spm.util.imcalc.options.mask = 0;
    matlabbatch{1}.spm.util.imcalc.options.interp = 1;
    matlabbatch{1}.spm.util.imcalc.options.dtype = 4;

    matlabbatch{2}.spm.spatial.smooth.data = {['m',wr_nii]};
    matlabbatch{2}.spm.spatial.smooth.fwhm = fwhm;
    matlabbatch{2}.spm.spatial.smooth.dtype = 0;
    matlabbatch{2}.spm.spatial.smooth.im = 0;
    matlabbatch{2}.spm.spatial.smooth.prefix = 's';

    spm_jobman('run', matlabbatch);

end

%%
%=======================================================================
% -Mask brain 
%=======================================================================
%% create folder 
mkdir mwr

%% Mask brain (batch)
wr_niis = spmk_filelist('.\wr\*.nii'); % get all wr nii files 
% set smooth kernal, and will be writen into the output file name
smooth_kernal = 10;
% main loops
for m =1:length(wr_niis)
    mwr_nii = ['.\mwr\','m',cell2mat(wr_niis(m))];
    smwr_nii = ['.\smwr\','s',num2str(smooth_kernal),'_m',cell2mat(wr_niis(m))];
    
    if ~exist(mwr_nii, 'file')
        mwrpet ={
                ['.\wr\', cell2mat(wr_niis(m))]
                'D:\MRI\Clinical\ctbrainmask_shrink.nii'};
        spmk_imcal(mwrpet,'i1.*(i2>0)',mwr_nii);
    end
    %
    spm_smooth(mwr_nii,smwr_nii,smooth_kernal);
end
%%
% Draw MIP for all nii files in current folder
mwr_niis = spmk_filelist('*.nii');
for m =1:length(mwr_niis)
    P = cell2mat(mwr_niis(m));
spmk_draw_mip(P,[0 3],[1 1 1],0,'jet',0);
end


%%
%=======================================================================
% FDG t-test
%=======================================================================
%% 
clear;clc;
folder_name = 'MOG_hc_ttest';
ttest_path = ['D:\MRIdataset\AFT\_FDG_all\',folder_name,'\'];
hc_list = spmk_filelist('D:\MRIdataset\AFT\_FDG_all\smwr\hc\rsmwr*.nii','fullpath');
pt_list =spmk_filelist('D:\MRIdataset\AFT\_FDG_all\smwr\pt\sm*-MOG.nii','fullpath');

spmk_pet_ttest(ttest_path, hc_list, pt_list)
%%
xjview('spmT_0001.nii')
%%
mask = 'D:\MRI\spm12\toolbox\mrtool\template\brainmask_mni.nii';
imgs = {'spmT_0002.nii'
    mask};
spmk_imcal(imgs, 'i1.*i2','m')

%%
%=======================================================================
% TSPO t-test
%=======================================================================
%% 
clear;clc;
folder_name = 'pt_hc_ttest';
ttest_path = ['D:\MRIdataset\AFT\_TSPO_all\',folder_name,'\'];
hc_list = spmk_filelist('D:\MRIdataset\AFT\_TSPO_all\smwr\hc\sm*.nii','fullpath');
pt_list =spmk_filelist('D:\MRIdataset\AFT\_TSPO_all\smwr\pt\sm*.nii','fullpath');

spmk_pet_ttest(ttest_path, hc_list, pt_list)
%%
xjview('spmT_0001.nii')
%%
mask = 'D:\MRI\spm12\toolbox\mrtool\template\brainmask_mni.nii';
imgs = {'spmT_0002.nii'
    mask};
spmk_imcal(imgs, 'i1.*i2','m')
%%
%=======================================================================
% Acetate t-test
%=======================================================================
%% 
clear;clc;
folder_name = 'hc_pt_ttest';
mkdir(folder_name)
ttest_path = ['D:\MRIdataset\AFT\_Acetate_all\',folder_name,'\'];
hc_list = spmk_filelist('D:\MRIdataset\AFT\_Acetate_all\smwr\hc\sm*.nii','fullpath');
pt_list =spmk_filelist('D:\MRIdataset\AFT\_Acetate_all\smwr\pt\sm*.nii','fullpath');

spmk_pet_ttest(ttest_path, hc_list, pt_list)
%%
xjview('spmT_0001.nii')
%%
mask = 'D:\MRI\spm12\toolbox\mrtool\template\brainmask_mni.nii';
imgs = {'spmT_0002.nii'
    mask};
spmk_imcal(imgs, 'i1.*i2','m')

%%
%=======================================================================
% -ROI based SUV extraction
%=======================================================================
%%
atlas = 'D:\AE\petimage\templates\rcb_aal_bs_WB.nii';
atlas = 'D:\AE\petimage\templates\rcb_aal_bs.nii'
aelist = spmk_filelist('*.nii');
spmk_extract_ROI_data(atlas,aelist,1,'IIDD-roi');
%%
%=======================================================================
% -Generate MIP img of ttest result
%=======================================================================
%%

%% mask MIP
clc
clear
nii_list = dir('single_*.nii');
for i=1:length(nii_list)
    nii = nii_list(i).name;
    mwrpet ={
        nii
        'D:\MRI\Clinical\ctbrainmask_shrink.nii'};
    
    spmk_imcal(mwrpet,'i1>9.92',['thr_',nii]);
    spmk_draw_mip(['thr_',nii],0,[0.5 0.5],[1 0 1]);
    
end
disp('done')


%%
spmk_draw_mip('s10_mwrtspo.nii',[0.01 1.0],[1 1 1],0,'jet');
%%  saggital 
bg = imread('snormed_ctbrainmask_shrink_2.nii.png');
nii_list = dir('snormed_thr_*.png');
for i=1:length(nii_list)
    a = imread(nii_list(i).name);
    c = bg;
    c(bg==255)=128;
    c(a==255)=255;
    imwrite(c,['bg_',nii_list(i).name])
end
%% axis
bg = imread('anormed_ctbrainmask_shrink_2.nii.png');
nii_list = dir('anormed_thr_*.png');
for i=1:length(nii_list)
    a = imread(nii_list(i).name);
    c = bg;
    c(bg==255)=128;
    c(a==255)=255;
    imwrite(c,['bg_',nii_list(i).name])
end
%%
%=======================================================================
% -Generate average normal people SUV 
%=======================================================================
%%

hc = {
    ['.\',src_path,'\Hs_fengyuzhou.nii']
    ['.\',src_path,'\Hs_shenjin.nii']
    ['.\',src_path,'\Hs_shenjun.nii']
    ['.\',src_path,'\Hs_zhangyin.nii']
%     ['.\',src_path,'\Hs_zhaoaiping.nii']
    };

% spmk_imcal(hc, '(i1+i2+i3+i4+i5)/5', 'TSPO_normal_5_suv')
spmk_imcal(hc, '(i1+i2+i3+i4)/4', 'TSPO_normal_4_suv')


%%

spmk_draw_mip(['gongjindi','.nii'],[0.8 0.8],[1 0 1],0,'jet');
    